package org.openems.otdr;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.openems.model.OtdrResultModel;
import org.slim3.datastore.Datastore;

@Path("/otdrFiles")
public class OtdrUplodResource {
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public OtdrSummaryEntities get(@QueryParam("wavelength") String wavelength, @QueryParam("pulsewidth") String pulsewidth) {
        return new OtdrTraceEntities().getOtdrTrace(wavelength, pulsewidth);
    }

    
    @POST
    @Consumes(MediaType.APPLICATION_OCTET_STREAM)
    @Produces(MediaType.TEXT_PLAIN)
    public String post(byte[] bin) {
        Sr4731Parser sr4731 = new Sr4731Parser(bin);
        OtdrResultModel model = new OtdrResultModel();
        model.setDistanceRange(sr4731.getDistanceRange());
        model.setPulseWidth(sr4731.getPulseWidth());
        model.setIor(sr4731.getIor());
        model.setWavelength(sr4731.getWavelength());
        model.setTrace(sr4731.getTraceInDB());
        Datastore.put(model);
        return "OK";
    }
}
